package com.itheima;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.junit.Test;

import java.util.Date;
import java.util.UUID;

public class JwtDemo {

    /**
     * 生成JWT
     */
    @Test
    public void testGenerate(){
        String jwt = Jwts.builder()
                .setId(UUID.randomUUID().toString()) //唯一标识
                .setSubject("ITCAST") //主题
                .claim("name", "itheima")//自定义信息-第二部分
                .claim("age", 88)     //自定义信息-第二部分
                .claim("role", "admin")//自定义信息-第二部分
                .setIssuedAt(new Date()) //签发日期
                .setExpiration(new Date())
                .signWith(SignatureAlgorithm.HS256, "nineclock")
                .compact();
        System.out.println(jwt);
    }

    /**
     * 校验
     */
    @Test
    public void verify(){
        String key = "eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJkZDQ1NDQ2NC01YjkzLTRkZjUtOTU1Yy0wMjI5MTE4YjQ3ZTYiLCJzdWIiOiJJVENBU1QiLCJuYW1lIjoiaXRoZWltYSIsImFnZSI6ODgsInJvbGUiOiJhZG1pbiIsImlhdCI6MTYxNzA3MDM2NCwiZXhwIjoxNjE3MDcwMzY0fQ.YMjEx027Cm0Gt8Ye3OHidOdG4BywsUsE6LuQIRKVVv0";

        Claims claims = Jwts.parser().setSigningKey("nineclock").parseClaimsJws(key).getBody();
        System.out.println(claims);
    }

}
